package org.tgcloud.group.service.task;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.tgcloud.group.core.RedisUtil;
import org.tgcloud.group.core.date.HolidayUtils;
import org.tgcloud.group.entity.SysDate;
import org.tgcloud.group.repository.SysDateMapper;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @author: zhy
 * @create: 2023-08-02 10:26
 * @description: 节假日
 */
@Component
public class DateTask {

    @Autowired
    private SysDateMapper dateMapper;

    private static String holidayKey = "holiday:";

//    @Scheduled(cron = "0 0 1 20 12 ?")
    public void doCheckDate() throws Exception{
        if (RedisUtil.existKey("doCheckDate")){
            return;
        }
        RedisUtil.setCacheObject("doCheckDate", "doCheckDate", 60L, TimeUnit.SECONDS);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String nowYear = sdf.format(new Date());
        Integer nowY = Integer.valueOf(nowYear);
        SysDate sysDate = dateMapper.selectLastTime();
        if (sysDate == null){
            doAddDate(nowY,1);
        }else {
            String year = sdf.format(sysDate.getDate());
            Integer y = Integer.valueOf(year);
            if (y+1 == nowY){
                doAddDate(y + 1, 1);
            }
        }
    }

    private void doAddDate(Integer year, Integer month) throws Exception{
        SimpleDateFormat sdfRedis = new SimpleDateFormat("yyyy:MM:dd");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年M月d日");
        String monthString = month < 10 ? ("0" + month) : month.toString();
        if (month > 12){
            //获取节假日名称
            LinkedHashMap yearMonth = HolidayUtils.getJjr(year, month);
            if ((Integer) yearMonth.get("code") == 0){
                Map<String, Map<String, Object>> holiday = (Map<String, Map<String, Object>>) yearMonth.get("holiday");
                Set<String> strings = holiday.keySet();
                for (String str : strings) {
                    Map<String, Object> stringObjectMap = holiday.get(str);
                    Boolean holidayBoolean = (Boolean) stringObjectMap.get("holiday");
                    String date = (String) stringObjectMap.get("date");
                    String holidayName = (String) stringObjectMap.get("name");
                    SimpleDateFormat sdfHolidayDate = new SimpleDateFormat("yyyy-MM-dd");
                    Date parse = sdfHolidayDate.parse(date);
                    SysDate sysDate = dateMapper.selectMsgByDate(sdf.format(parse));
                    if (null != sysDate){
                        sysDate.setHolidayName(holidayName);
                        dateMapper.updateByPrimaryKeySelective(sysDate);
                        String format = sdfRedis.format(sysDate.getDate());
                        String key = holidayKey + format;
                        RedisUtil.setCacheObject(key, sysDate);
                    }
                }
            }
        }else {
            LinkedHashMap allDay = HolidayUtils.getAllDay(year, month);
            Map<String, Integer> map = (Map) allDay.get(year + monthString);
            for (String key : map.keySet()){
                SimpleDateFormat sdfKey = new SimpleDateFormat("yyyyMMdd");
                Integer value = map.get(key);
                SysDate sysDate = new SysDate();
                sysDate.setDate(sdfKey.parse(year + key));
                sysDate.setDateName(sdf.format(sysDate.getDate()));
                sysDate.setDayType(value);
                dateMapper.insertSelective(sysDate);
                String format = sdfRedis.format(sdfKey.parse(year + key));
                String redisKey = holidayKey + format;
                if (!RedisUtil.existKey(redisKey)){
                    RedisUtil.setCacheObject(redisKey, sysDate);
                }
            }
            doAddDate(year, month + 1);
        }
    }



}
